﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using XmlParser;
using System.IO;

namespace CommonUtilities.Factories
{
    public class XmlFactory<T> where T : IXmlObject
    {
        public IXmlParser xmlParser;

        public string BaseNamespace { get; set; }
        public string OutputFolderName { get; set; }

        public T CreateXmlRootNode(string xmlFileName)
        {
            this.xmlParser = new XmlParser.XmlParser(this.BaseNamespace);

            string path = AppDomain.CurrentDomain.BaseDirectory;
            string OutputPath = Path.Combine(Path.Combine(path, OutputFolderName), xmlFileName + ".xml");
            this.xmlParser.XmlLoad(OutputPath, "");
            
            Type t = System.Type.GetType(this.BaseNamespace + xmlParser.RootElementName + ".Xml" + xmlParser.RootElementName);
            this.xmlParser.XmlParse(t.Assembly.FullName);

            T xmlRoot = (T)this.xmlParser.RootElement;
            return xmlRoot;
        }
    }
}